<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\OrderLogic;
class Order extends Common
{
	public function _initialize(){

	}
	public function index(){
		$begin = date('Y/m/d',(time()-12*30*60*60*24));
    	$end   = date('Y/m/d',strtotime('+1 days'));
    	$this->assign('timegap',$begin.'-'.$end);
    	$this->assign('begin',$begin);
    	$this->assign('end',$end);
    	$this->assign('user_id',input('get.user_id'));
        return view();
	}

	/**
     * 异步获取订单列表
     * FunctionName: ajaxIndex
     * @author : roc
     */
    public function ajaxIndex(){
        $orderLogic = new OrderLogic;       
        $timegap = input('timegap');
        if($timegap){
        	$gap = explode('-', $timegap);
        	$begin = strtotime($gap[0]);
        	$end = strtotime($gap[1]);
        }
        // 搜索条件
        $condition = array();
        $condition['is_del'] = 0;
        input('consignee') ? $condition['consignee'] = trim(input('consignee')) : false;
        if($begin && $end){
        	$condition['add_time'] = array('add_time','between',"$begin,$end");
        }
        input('order_sn') ? $condition['order_sn'] = trim(input('order_sn')) : false;
        input('order_status')  != '' ? $condition['order_status'] = input('order_status') : false;
        input('pay_status')    != '' ? $condition['pay_status'] = input('pay_status') : false;
        input('shipping_status') != '' ? $condition['shipping_status'] = input('shipping_status') : false;
        input('user_id') != '' ? $condition['user_id'] = input('user_id') : false;

        $sort_order = input('order_by','DESC').' '.input('sort');
        // dump($condition);exit;
        $count = db('order')->where($condition)->count();// 查询满足要求的总记录数

        $size = 20;

        $orderList = db('order')->where($condition)->order($sort_order)->paginate($size,$count)->each(function($item,$key){
        	return $item;
        });
        $show = $orderList->render();

        $this->assign('orderList',$orderList);
        $this->assign('page',$show);// 赋值分页输出
        return view();
    }

    /**
     * 订单详情
     * @param int $id 订单id
     */
    public function detail($order_id){
        $id = input('route.order_id');
        $orderInfo = db('order')->where(array('order_id'=>$id))->find();
        $orderInfo['zhifu_url'] = url('Order/payOrder',array('order_id'=>$id));
        $orderInfo['end_url'] = url('Order/endOrder',array('order_id'=>$id));
        $orderInfo['fahuo_url'] = url('Order/shippingOrder',array('order_id'=>$id));
            switch ($orderInfo['order_status']) {
                case '0':
                    $orderInfo['zhuangtai'] = '未完成';
                    break;
                case '1':
                    $orderInfo['zhuangtai'] = '已完成';
                    break;
                case '2':
                    $orderInfo['zhuangtai'] = '已取消';
                    break;
                default:
                    $orderInfo['zhuangtai'] = '未完成';
                    break;
            }

        $orderGoods = db('order_goods')->where(array('order_id'=>$id))->select();

        $action_log = db('order_action')->where(array('order_id'=>$id))->order('add_time desc')->select();
        foreach ($action_log as $key => $value) {
            # code...
            $action_log[$key]['admin_user'] = db('admin')->where(array('admin_id'=>$value['user']))->column('user_name');
        }

        // dump($action_log);
        $this->assign(array(
            'orderInfo' => $orderInfo,
            'orderGoods' => $orderGoods,
            'action_log' => $action_log,
        ));
        
        return view();
    }


    /**
    ** 修改订单支付的状态
    **/
    function payOrder(){
        $post_data = input('post.');
        $id = input('post.id');
        // $order = M('Order');
        if($id){
            $orderInfo = db('order')->where(array('order_id'=>$id))->find();
            if(!$orderInfo){
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
            if($orderInfo['pay_status'] == 0){
                $re = db('order')->where(array('order_id'=>$id))->update(array('pay_status'=>1,'admin_note'=>$post_data['note'],'pay_time'=>time()));
                if($re !== false){
                    $action = array(
                        'order_id' => $id,
                        'user' => $this->manger_info['user_name'],
                        'user_name' => $this->manger_info['user_name'],
                        'add_time' => time(),
                        'note' => $post_data['note'],
                        'text' => '后台管理员操作,支付',
                    );
                    db('order_action')->insertGetId($action);//添加订单操作记录
                    $json = array('status'=>1,'message'=>'操作成功','loading'=>1);
                    echo json_encode($json);return;
                }else{
                    $json = array('status'=>0,'message'=>'操作失败');
                    echo json_encode($json);return;
                }
            }else{
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
        }else{
            $json = array('status'=>0,'message'=>'操作失败');
            echo json_encode($json);return;
        }
    }

    /**
    ** 修改订单完成的状态
    **/
    function endOrder(){
        $post_data = input('post.');
        $id = input('post.id');
        // $order = M('Order');
        if($id){
            $orderInfo = db('order')->where(array('order_id'=>$id))->find();
            if(!$orderInfo){
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
            if($orderInfo['order_status'] == 0){
                $re = db('order')->where(array('order_id'=>$id))->update(array('order_status'=>1,'admin_note'=>$post_data['note']));
                if($re !== false){
                    $action = array(
                        'order_id' => $id,
                        'user' => $this->manger_info['user_name'],
                        'user_name' => $this->manger_info['user_name'],
                        'add_time' => time(),
                        'note' => $post_data['note'],
                        'text' => '后台管理员操作,修改订单为完成状态',
                    );
                    db('order_action')->insertGetId($action);//添加订单操作记录
                    $json = array('status'=>1,'message'=>'操作成功','loading'=>1);
                    echo json_encode($json);return;
                }else{
                    $json = array('status'=>0,'message'=>'操作失败');
                    echo json_encode($json);return;
                }
            }else{
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
        }else{
            $json = array('status'=>0,'message'=>'操作失败');
            echo json_encode($json);return;
        }
    }

    /**
    ** 订单发货
    **/
    public function shippingOrder(){
        $post_data = input('post.');
        $id = input('post.id');
        // $order = M('Order');
        if($id){
            $orderInfo = db('order')->where(array('order_id'=>$id))->find();
            if(!$orderInfo){
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
            if($orderInfo['shipping_status'] == 0){
                if(!$post_data['shipping_code'] || !$post_data['shipping_name']){
                    $json = array('status'=>0,'message'=>'请填写发货信息');
                    echo json_encode($json);return;
                }
                $data = array('shipping_status'=>1,'shipping_code'=>$post_data['shipping_code'],'shipping_name'=>$post_data['shipping_name'],'shipping_time'=>time());
                $re = db('order')->where(array('order_id'=>$id))->update($data);
                if($re !== false){
                    $action = array(
                        'order_id' => $id,
                        'user' => $this->manger_info['user_name'],
                        'user_name' => $this->manger_info['user_name'],
                        'add_time' => time(),
                        'note' => '物流：'.$post_data['shipping_name'].'，单号：'.$post_data['shipping_code'],
                        'text' => '后台管理员操作,发货',
                    );
                    db('order_action')->insertGetId($action);//添加订单操作记录
                    $json = array('status'=>1,'message'=>'操作成功','loading'=>1);
                    echo json_encode($json);return;
                }else{
                    $json = array('status'=>0,'message'=>'操作失败');
                    echo json_encode($json);return;
                }
            }else{
                $json = array('status'=>0,'message'=>'操作失败','loading'=>1);
                echo json_encode($json);return;
            }
        }else{
            $json = array('status'=>0,'message'=>'操作失败');
            echo json_encode($json);return;
        }
    }

    /**
     * 订单删除
     * @param int $id 订单id
     */
    public function delete_order($id){
        $orderLogic = new OrderLogic;
        // $del = $orderLogic->delOrder($id);
        $del = db('order')->where(array('order_id'=>$id))->update(array('is_del'=>1));
        if(request()->isAjax()){
            if($del !== fasle){
                $return_arr = array('status' => 1, 'msg'   => '删除订单成功', 'data'  => '');
            }else{
                $return_arr = array('status' =>-1, 'msg'   => '订单删除失败', 'data'  => '');
            }
            // $this->ajaxReturn(json_encode($return_arr));
            echo json_encode($return_arr);exit;
        }else{
            if($del !== fasle)
                $this->success('移去成功',url('Order/index'));
            else
                $this->error('移去失败',url('Order/index'));
        }

    }












}